<?php
	error_reporting(E_ALL || ~E_NOTICE);
//	$controlerpagenum = 1;
	$exportStr = "";

	$headerArr = array("accessrecord"=>"tkid,cardnum,cardlen,controlerNum,gatenum,CreatedTime\n",
						"attendanceform"=>"skid,staffnum,staffname,departmentname,latetimes,earlytimes,overtimes,absentofwork,offtimes\n"
		);


    $table = $_GET["table"];

	$myconn=@mysql_connect("localhost","root","root");
	if(!$myconn)
	{
		die('Could not connect: '.mysql_error());
	}

	$db_selected=mysql_select_db("dwaccessdb",$myconn);
	if($db_selected)
	{
		//echo "111\r\n";
//		$filename = "table.csv";
		$temp =  "fields terminated by ',' optionally enclosed by '\"' lines";
		// @file_put_contents("test.log", "111:", FILE_APPEND);
		// @file_put_contents("test.log", $table, FILE_APPEND);
		// @file_put_contents("test.log", "222\r\n", FILE_APPEND);
		// die('ok111: '.mysql_error());
		$strSql="select * from  $table";
		//die('ok111: '.$strSql);
		//echo $strSql;
		$result=mysql_query($strSql,$myconn);

		while($rowsnum = mysql_fetch_array($result,MYSQL_NUM))
		{
			//var_dump($rowsnum);
			foreach ($rowsnum as $key => $value) 
			{

				// echo('ok111: '.$key);
				// echo('ok222: '.count($rowsnum));
				//echo "11".$value."\r\n";
				$temp = iconv('GBK', 'utf-8', $value);
				if($key == count($rowsnum)-1)
				{
					//die("11 : ".$value);
					$exportStr = $exportStr . $temp . "\n";
				}
				else
				{
					$exportStr = $exportStr . $temp . ",";
				}
				
			}
		}	
	   
	}else
	{
		die("Can't use database dwaccessDB : ".mysql_error());
		$string = "Can't use database : ".mysql_error();
//          file_put_contents("test.log", $string, FILE_APPEND);
	}		
	  
	// die("11 : ".$headerArr["accessrecord"]);

	$file_name = $table . '.csv';
	//@file_put_contents("test.log", $file_name, FILE_APPEND);
	//header("Content-type:text/csv;");
	//header("Content-type:application/octet-stream;");
	header("Content-Type: application/vnd.ms-excel; charset=GB2312");
	header("Content-Disposition:attachment;filename=" . $file_name);
	//header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
	//header('Expires:0');
	//header('Pragma:public');
	//die("11 : ".$headerArr["accessrecord"]);
	echo $headerArr[$table];
	echo $exportStr; 
	mysql_free_result($result);
	mysql_close($myconn);	

?>
